<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>   
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>  
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>  
<%@ taglib uri="http://shiro.apache.org/tags" prefix="shiro" %>     
<c:set var="ctx" value="${pageContext.request.contextPath}"/> 
<%@ taglib prefix="dict" uri="/WEB-INF/tld/dict.tld" %>  
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<jsp:include page="../include.jsp"/>
<script type="text/javascript">
	var dataGrid;
	$(function() {
		itemsShift = <dict:getDictItem dictCode="DICT_WORK_SHIFT"/>;
		setHeight();
		dataGrid = $('#dg').datagrid({
			title:'机台生产记录',
			url:'${ctx}/prodMstr/listProdMstrs',
			nowrap:true,
			striped:true,
			idField:'pmId',
			pagination:true,
			fit:true,
			fitColumns:true,
			sortName:'MACH_NUM,SHIFT,PRO_DATE',
			sortOrder:'asc,asc,asc',
			rownumbers: true, //是否加行号 
			columns:[[	   
				 {field:'pmId',checkbox :true},
	    		 {field:'machNum',title:'机台号',width:50,align:'left'},
	    		 {field:'itemNo',title:'产品名称',width:50,align:'left'},
	    		 {field:'shift',title:'班次',width:50,align:'left',formatter:formatterShift},
	    		 {field:'staffId',title:'员工工号',width:50,align:'left'},
	    		 {field:'targetQty',title:'目标产量',width:50,align:'left'},
	    		 {field:'proDate',title:'生产日期',width:50,align:'left',formatter:formatterProDate}
			]],
			toolbar : '#toolbar',
			onHeaderContextMenu: function(e, field){
                e.preventDefault();
                $(this).datagrid('columnMenu').menu('show', {
                    left:e.pageX,
                    top:e.pageY
                });
            }
		}).datagrid('columnMoving');
		
	});
	
	function add() {
		parent.$.modalDialog({
			title:'添加目标产量',
			iconCls:'icon-save',
			href:'${ctx}/prodMstr/toProdMstrAdd',
			width:'60%',
			height:'90%',
			resizable:true,
			collapsible:true,
			maximizable:true,
			buttons:[{
				text:'提交',
				iconCls:'icon-ok',
				handler:function() {
					parent.$.modalDialog.openner_dataGrid = dataGrid;//因为添加成功之后，需要刷新这个dataGrid，所以先预定义好
					var f = parent.$.modalDialog.handler.find('#form');
					f.submit();
				}
			},{
				text:'取消',
				iconCls:'icon-cancel',
				handler:function() {
					parent.$.messager.confirm('系统提示', '确定要关闭当前窗口?', function(r){
		                if (r){
		                	parent.$.modalDialog.handler.dialog('close');
		                }
		            });					
				}
			}]
		});
	}
	
	function doSearch(){
		var params=$('#queryForm').serializeObject();
		$('#dg').datagrid('load',params);  
	}
	
	function doClear(){
		$('#queryForm').form("clear");
	}
	
	
	function batchDel(){
		var rows = dataGrid.datagrid('getChecked');
		var ids = [];
		if (rows.length > 0) {
			parent.$.messager.confirm('确认', '您是否要删除当前选中的项目？', function(b){ if(b){										
					$.each(rows,function(n,row) {			           
			            ids.push(row.pmId);
			        });					
					$.ajax({
	   					type: "POST",
	   					url: '${ctx}/prodMstr/deleteProdMstr',
	   					dataType:"json",
	   					data:{
	   						ids : ids.join(',')
	   					},
	   					success: function(result) {	   						
	     					if(result.success == true) {	     						
								parent.$.messager.alert('提示', result.msg, 'info');								
								dataGrid.datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
								dataGrid.datagrid('load');
	     					} else {
	     						parent.$.messager.alert('错误', result.msg, 'error');		     						
	     					}	     				
		   				}
					});
					parent.$.messager.progress('close');	
				}
			});
		} else {
			parent.$.messager.alert('系统提示', '请勾选要删除的记录！', 'warning');
		}
	}
	
	
	function udpate(id){
		if (typeof(id) == 'undefined') {
			var rows = dataGrid.datagrid('getSelections');				
			if(rows.length != 1){
				parent.$.messager.alert('系统提示', '请选择一条记录！', 'warning');
				return;
			}
			id = rows[0].pmId;	
		} else {
			dataGrid.datagrid('unselectAll').datagrid('uncheckAll');
		}
		parent.$.modalDialog({
			title:'编辑目标产量',
			iconCls:'icon-edit',
			width:'60%',
			height:'90%',
			resizable:true,
			collapsible:true,
			maximizable:true,
			href:'${ctx}/prodMstr/toProdMstrAdd?pmId='+id,
			buttons:[{
				text:'修改',
				iconCls:'icon-ok',
				handler : function() {
					parent.$.modalDialog.openner_dataGrid = dataGrid;//因为添加成功之后，需要刷新这个dataGrid，所以先预定义好
					var f = parent.$.modalDialog.handler.find('#form');
					f.submit();
				}
			},{
				text:'取消',
				iconCls:'icon-cancel',
				handler:function(){
					parent.$.messager.confirm('系统提示', '确定要关闭当前窗口?', function(r){
		                if(r){
		                	parent.$.modalDialog.handler.dialog('close');
		                }
		            });					
				}
			}]
		});
	}
	
	function formatterProDate(value,row,index){
		if(value){
			return value.substr(0,10);
		}
		return value;
	}
	
	function formatterShift(value, row, index){
   		var val =  $.getDictItemText(itemsShift,value);
   		return val;
   	}
	
	function setHeight(){
		var c = $('#cc');
		var p = c.layout('panel','north');	// get the north panel
		var oldHeight = p.panel('panel').outerHeight();
		p.panel('resize', {height:'auto'});
		var newHeight = p.panel('panel').outerHeight();
		c.layout('resize',{
			height: (c.height() + newHeight - oldHeight)
		});
	}
</script>
</head>
<body>
	<div id="cc" class="easyui-layout" data-options="fit:true">
	    <div data-options="region:'north',border:false" style="height:50px;padding-top:10px;padding-left:5px">	        
	        <form id="queryForm" method="post">			
				<div style="margin-bottom:1px;padding-left:5px">
					<input class="easyui-textbox" name="machNum" labelAlign="right" labelWidth="100px" label="机台号:" labelPosition="left" data-options="" style="width:20%;height:22px;">
					<select class="easyui-combobox" name="shift" labelAlign="right" labelWidth="100px" label="班次:"labelPosition="left" editable="false" style="width:20%;height:24px;">
						<option value="">全部</option>							
						<c:forEach items="${dictShiftItems}" var="item">
							<option value="${item.itemValue}" >${item.itemText}</option>
						</c:forEach>
					</select>
					<input class="easyui-datebox" name="proDateFrom" labelAlign="right" label="起始生产日期:" labelWidth="100px" labelPosition="left" data-options="" style="width:20%;height:22px;">
				</div>
				<div style="margin-bottom:1px;padding-top:1px;padding-left:5px">
					<input class="easyui-textbox" name="itemNo" labelAlign="right" labelWidth="100px" label="产品名称：" labelPosition="left" data-options="" style="width:20%;height:22px;">
					<input class="easyui-textbox" name="staffId" labelAlign="right" labelWidth="100px" label="员工工号:" labelPosition="left" data-options="" style="width:20%;height:22px;">
				 	<input class="easyui-datebox" name="proDateTo"  labelAlign="right" label="截止生产日期:" labelWidth="100px" style="width:20%;height:22px;" >
					<a onclick="doSearch();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-search'">查询</a>
					<a onclick="doClear();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-reload'">重置</a>				
				</div>
			</form>
	    </div>
		<div data-options="region:'center',border:false">
		     <table id="dg"></table>
		 </div>
	</div>
	
	<div id="toolbar" style="display: none;">
		<a onclick="add();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-add'">添加</a>
		<a onclick="udpate();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-edit'">修改</a>
		<a onclick="batchDel();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-remove'">删除</a>
	</div>
</body>
</html>